package cn.com.inossem.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.com.inossem.activity.R;
import cn.com.inossem.model.MessageInfoBean;
import cn.com.inossem.utils.StringUtil;
import cn.com.inossem.utils.Utils;

public class XiTongMessageListAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private List<MessageInfoBean> messageInfo;
	private Handler handler;

	public XiTongMessageListAdapter(Context mContext,
			List<MessageInfoBean> messageInfo, Handler handler) {
		super();
		this.mContext = mContext;
		this.messageInfo = messageInfo;
		this.handler = handler;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		// return messageInfo.size();
		return messageInfo.size();
	}

	@Override
	public MessageInfoBean getItem(int arg0) {
		// return messageInfo.get(arg0);
		return messageInfo.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.message_item_layout, null);
			holder.rlayout = (RelativeLayout) convertView
					.findViewById(R.id.rlayout);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.addfriend = (TextView) convertView
					.findViewById(R.id.addfriend);
			holder.img = (ImageView) convertView.findViewById(R.id.img);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (getItem(position).getUseType().equals("13238")) {
			holder.time.setVisibility(View.VISIBLE);
			holder.addfriend.setVisibility(View.GONE);
			holder.content.setVisibility(View.VISIBLE);
			holder.content.setText(getItem(position).getContent());
			holder.time.setText(getItem(position).getCreateTime());
		} else if (getItem(position).getUseType().equals("13239")) {
			holder.time.setVisibility(View.VISIBLE);
			holder.addfriend.setVisibility(View.GONE);
			holder.content.setVisibility(View.VISIBLE);
			holder.content.setText(getItem(position).getContent());
			holder.time.setText(getItem(position).getCreateTime());
		} else if (getItem(position).getUseType().equals("13240")) {
			holder.time.setVisibility(View.GONE);
			holder.addfriend.setVisibility(View.VISIBLE);
			holder.content.setVisibility(View.VISIBLE);
			holder.content.setText(getItem(position).getContent());
			holder.title.setText(getItem(position).getApplyFriend()
					.getTruename());
			if (!getItem(position).getApplyFriend().getImageData().equals(""))
				holder.img.setBackgroundDrawable(new BitmapDrawable(Utils
						.toRoundCorner(stringtoBitmap(getItem(position)
								.getApplyFriend().getImageData()), 50)));
			holder.rlayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
//					Intent intent = new Intent(mContext,
//							FriendsDetailsActivity.class);
//					intent.putExtra("type", 1);
//					intent.putExtra("toUserId", getItem(position).getUserID());
//					intent.putExtra("toUserName", u_list.get(arg2)
//							.getNickname());
//					intent.putExtra("phone", getItem(position).get);
//					mContext.startActivity(intent);
				}
			});

			holder.addfriend.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Message me = new Message();
					me.what = 1;
					Bundle bun = new Bundle();
					System.out.println("userid："
							+ getItem(position).getUserID());
					bun.putString("friendid", getItem(position)
							.getApplyFriendID() + "");
					me.setData(bun);
					handler.sendMessage(me);
				}
			});
		} else {
			String createTime = getItem(position).getCreateTime();
			String content = getItem(position).getContent();
			if (StringUtil.isNotBlank(createTime)) {
				holder.time.setVisibility(View.VISIBLE);
				holder.time.setText(createTime);
			}
			if (StringUtil.isNotBlank(content)) {
				holder.content.setVisibility(View.VISIBLE);
				holder.content.setText(content);
			}
		} 
		return convertView;
	}

	public Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
					bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}

	public final class ViewHolder {
		RelativeLayout rlayout;
		public ImageView img;
		public TextView title;
		public TextView content;
		public TextView time;
		public TextView addfriend;
	}
}
